#ifndef MONO_MEMORY_MANAGER_H_
#define MONO_MEMORY_MANAGER_H_
#include <fermat/common/atomic.h>
#include <algorithm>
#include <cassert>

namespace  mono {

class Manager {
public:
	Manager(Manager *father, const std::string &name):_father(father), _name(name)
	{

	}
	~Manager();
	Manager * super() { return _father; }

	size_t total() const { return _total; }

	size_t maxAllocted() const { return _peek; }

	Manager& add(size_t n)
	{
		size_t current = (_total += n);
		_peek = std::max(_peek.load(), current);
		++_alloc_count;
		if(_father) {
			_father->add(n);
		}
		return *this;
	}

	Manager& sub(size_t n)
	{
		assert(_total > n);
		_total -= n;
		if(_father) {
			_father->sub(n);
		}
		return *this;
	}
private:
	Manager              *_father;
	std::string           _name;
	std::atomic<size_t>   _alloc_count;
	std::atomic<size_t>   _total;
	std::atomic<size_t>   _peek;


};

} //namespace mono
#endif